﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_HealingWaveBuff : Buff
    {
        public Spell_HealingWaveBuff() : base() 
        {
            minutesLeft = 1;
            ticksLeft = 40;
            spellTimer.Interval = 1500;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            ticksLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            
            spellTimer.Start();
        }
        override protected void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            ticksLeft--;

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (TargetCreatureBase.HP < 1)
                {
                    RemoveSpell(true, false);
                    return;
                }

                if (TargetCreatureBase.HP < TargetCreatureBase.MaxHP)
                {
                    TargetCreatureBase.HP = TargetCreatureBase.HP + 20;
                    TargetCreatureBase.Update_Character();

                    Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                    foreach (MapItem mapitem in CurrentMap.MapItems)
                    {
                        if ((mapitem.MapX >= TargetCreatureBase.MapX - 4 && mapitem.MapX <= TargetCreatureBase.MapX + 4) && (mapitem.MapY >= TargetCreatureBase.MapY - 4 && mapitem.MapY <= TargetCreatureBase.MapY + 4))
                        {
                            foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                            {
                                if (creatureBase is Character)
                                {
                                    Character character = (Character)creatureBase;
                                    character.UpdateCreatureBaseHp(TargetCreatureBase);
                                }
                            }
                        }
                    }
                }
            }
            if (ticksLeft == 0)
            {
                RemoveSpell(true, true);
                return;
            }
            else
                spellTimer.Start();
        }
        
    }
}
